import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {HttpService} from "../../providers/HttpService";
import {InfoProvider} from "../../providers/InfoProvider";
import {CommProvider} from "../../providers/CommProvider";

/**
 * Generated class for the PubTradePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-pub-trade',
  templateUrl: 'pub-trade.html',
})
export class PubTradePage {

  trade_content: string[] = [
      '',
      '买入价格: ',
      '卖出价格: ',
      '平仓价格: '
  ];

  pub_trade: any = {
    type: 0,
    price: null,
    content: null,
    user_id: null,
    user_code: null
  };

  user_info: any;

  constructor(public navCtrl: NavController, public navParams: NavParams,
              private commProvider: CommProvider,
              private infoProvider: InfoProvider,
              private httpService: HttpService) {
    this.user_info = this.infoProvider.getUserInfo();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PubTradePage');
  }

  onPubTrade(type: number) {
    this.pub_trade.type = type;
    this.pub_trade.user_id = this.user_info.id;
    this.pub_trade.user_code = this.user_info.code;
    console.log('onPubTrade: ', this.pub_trade);

    if (!this.pub_trade.price) {
      this.commProvider.showToast('缺少交易价格');
      return;
    }

    this.pub_trade.content = this.trade_content[type] + this.pub_trade.price + '元';
      this.commProvider.showConfirmAlert('发布交易信息', this.pub_trade.content)
          .then(confirm => {
              console.log('showConfirmAlert done');
              if (confirm) {
                  this.httpService.instance()
                      .setUrl('/vipTrade/addVipTrade')
                      .setBody(this.pub_trade)
                      .postRequest()
                      .then(res => {
                          if (res.status == 0) {
                              this.commProvider.showToast('发布成功');
                              this.navCtrl.getActive().dismiss({
                                  type: type,
                                  data: res.data
                              });
                          } else {
                              this.commProvider.showToast(res.msg);
                          }
                      });
              }
          });
  }
}
